/*
 * Main.fx
 *
 * Created on 2010-11-13, 12:51:04
 */

package pl.elka.gis.main;

import javafx.stage.Stage;
import javafx.scene.Scene;
import pl.elka.gis.main.AppMediator;
import pl.elka.gis.main.view.PaintableNode;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.control.Button;
import javafx.scene.layout.Panel;
import javafx.scene.Group;

import java.lang.System;
import javafx.scene.control.Label;

import pl.elka.gis.main.node.Node;
import javafx.scene.shape.Line;
import javafx.scene.paint.Color;
import pl.elka.gis.main.node.Edge;
import pl.elka.gis.main.view.PaintableEdge;

/**
 * @author looser
 */
var stageW:Float = 900;
var stageH:Float = 650;

var hints:PaintingHints = AppMediator.getInstance().countPaintingHints();
hints.setMapH(stageH-120);
hints.setMapW(stageW-100);

var nodes =  AppMediator.getInstance().getAllNodes().toArray();
var edges = AppMediator.getInstance().getAllEdges().toArray();
var nodesMap = AppMediator.getInstance().getNodesMap();
var nodesAray =
    for(i in [0..nodes.length-1]){
        PaintableNode{
            node:nodes[i] as Node;
            hints:hints
        }
     }
var edgesAray =
    for(i in [0..edges.length-1]){
            var e = edges[i] as Edge;
            PaintableEdge{
               nodeFrom: nodesMap.get(e.getFrom()),
               nodeTo:nodesMap.get(e.getTo()),
               hints:hints
            }            
     }
     
var nodesPanel = Panel{
    content: [
        edgesAray,
        nodesAray
     ]
 }

var msgTB = Label {
    text: ""    
}

var stageContent = Group{
            content: [
                nodesPanel
            ]
        }
var funcPanel = VBox{
    spacing:10
    content: [
        VBox{
            spacing:10            
            content: [
                HBox{                   
                   spacing:10
                   content:[
                        Button{
                            action:function(){clearColors();}
                            text:"Wyszyść"
                        }
                        
                        Button{
                            action:function(){colorEdges();}
                            text:"Koloruj"
                        }

                        Button{
                            action:function(){reloadFile();}
                            text:"Przeładuj plik"
                        }
                    ]
                },               
                msgTB
            ]
        }
    ]
}

Stage {
    title: "Kolorowanie grafu"
    scene: Scene {
        width: stageW, height: stageH
        content: [
            VBox{
                layoutX: 10;layoutY: 10;
                content: bind [funcPanel,stageContent]
            }
        ]             
    }
}


function clearColors(){
   edgesAray =
    for(i in [0..edges.length-1]){
            var e = edges[i] as Edge;
            PaintableEdge{
               nodeFrom: nodesMap.get(e.getFrom()),
               nodeTo:nodesMap.get(e.getTo()),
               hints:hints
            }
     }
    nodesPanel.content = [
        edgesAray,
        nodesAray
            ];
    msgTB.text = "";
}
function reloadFile(){
    AppMediator.getInstance().initNodes();

    nodes =  AppMediator.getInstance().getAllNodes().toArray();
    edges = AppMediator.getInstance().getAllEdges().toArray();
    nodesMap = AppMediator.getInstance().getNodesMap();
    nodesAray =
    for(i in [0..nodes.length-1]){
        PaintableNode{
            node:nodes[i] as Node;
            hints:hints
        }
     }
    clearColors();
}

function colorEdges(){
    var startTime = System.currentTimeMillis();
    var coloredEdges = AppMediator.getInstance().solve().toArray();
    var endTime = System.currentTimeMillis();
    msgTB.text = "Ilość wierzchołków {AppMediator.getInstance().getAllNodes().size()}\nIlość krawędzi {AppMediator.getInstance().getAllEdges().size()}\nKolorowanie trwało {endTime - startTime} milisekund.\n Suma chromatyczna wynosi {AppMediator.getInstance().getSumaSmigania()}\n Wynik zostal zapisany w pliku 'out.csv'";

    edgesAray =
    for(i in [0..coloredEdges.length-1]){
            var e = coloredEdges[i] as Edge;
            PaintableEdge{
               nodeFrom: nodesMap.get(e.getFrom()),
               nodeTo:nodesMap.get(e.getTo()),
               hints:hints,
               color:e.getColor()
            }
     }

    nodesPanel.content = [
        edgesAray,
        nodesAray
            ]
    
}
